<?php
/**
 * Created by PhpStorm.
 * User: apple
 * Date: 2020/10/12
 * Time: 4:06 PM
 */
namespace app\index\logic;


use app\event\ModifyIntegral;
use app\model\IntegralLog;
use app\model\User;
use app\model\UserReport;
use app\model\UserVideo;
use app\model\Video;
use think\facade\Db;
use think\facade\Request;

class UserReportLogic
{

    /*
     * 获取报告列表 默认周报
     */
    public function getReportList(){

        $orgId = config('user')['orgid'];
        $uid = config('user')['id'];

        $param = Request::get();
        $where = [
            ['ur_uid','=',$uid],
            ['ur_orgid','=',$orgId],
            ['ur_status','=',1],
        ];

        if(isset($param['type']) && !empty($param['type'])){
            $where[] = ['ur_type','=',intval($param['type'])];
        }else{
            $where[] = ['ur_type','=',1];
        }

        if(isset($param['startTime']) && !empty($param['startTime'])){
            $where[] = ['ur_create_time','>=',strtotime(date('Y-m-d',$param['startTime']))];
        }

        if(isset($param['endTime']) && !empty($param['endTime'])){
            $where[] = ['ur_create_time','<=',strtotime(date('Y-m-d 23:59:59',$param['endTime']))];
        }

        $reportModel = new UserReport();

        return $reportModel->getReportListByCondition($where);
    }

    /*
     * 提交 月报、周报
     */
    public function add(){

//        if(!position(User::CLEANING_CHARGE))  fail('提交失败，暂无权限');

        $param = Request::post();

        $data = [
            'ur_uid' => config('user')['id'],
            'ur_orgid' => config('user')['orgid'],
        ];
//        if(!isset($param['title']) || empty($param['title']))   fail('请填写报告标题');
//        $data['ur_title'] = trim($param['title']);

        if(!isset($param['content']) || empty($param['content']))   fail('请填写报告内容');
        $data['ur_content'] = json_encode($param['content']);

        if(!isset($param['type']) || empty($param['type']))   fail('请选择正确的报告类型');
        $data['ur_type'] = intval($param['type']);
        $data['ur_title'] = '';

        $UserReportModel = new UserReport();
        $rt = $UserReportModel->addOneReport($data);
        if(!$rt)    fail('添加失败');

        success();
    }

    /*
     * 判断报告是否提交过
     */
    public static function checkReportCreated($uid,$type = 1){

        $startTime = strtotime("this week");
        if($type == 2){
            //月报
            $startTime = strtotime(date('Y-m-01'));
        }
        $where = [
            ['ur_uid','=',$uid],
            ['ur_type','=',$type],
            ['ur_create_time','>=',$startTime],
            ['ur_create_time','<=',time()],
        ];

        $userReportModel = new UserReport();
        $info = $userReportModel->getReportInfoByWhere($where,'ur_id');

        if(!$info)   return false;

        return true;
    }
}